﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Phidgets;
using Phidgets.Events;

using System.Threading;

namespace LorraineSec.Libraries.Sensors
{
    /// <summary>
    /// Library for interfacing with the PhidgetsRFID sensor via USB.
    /// </summary>
    public sealed class PhidgetUSBRFID_Lib : Sensor
    {
        private RFID rfidListen;

        public PhidgetUSBRFID_Lib() { }

        public override bool initiate()
        {
            try
            {
                rfidListen = new RFID();

                rfidListen.Attach += new AttachEventHandler(rfidListen_Attach);
                rfidListen.Detach += new DetachEventHandler(rfidListen_Detach);
                rfidListen.Error += new ErrorEventHandler(rfidListen_Error);

                rfidListen.Tag += new TagEventHandler(rfidListen_Tag);
                rfidListen.TagLost += new TagEventHandler(rfidListen_TagLost);

                rfidListen.OutputChange += new OutputChangeEventHandler(rfidListen_OutputChange);

                rfidListen.open(-1);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public override bool shutdown()
        {
            rfidListen.close();
            return true;
        }

        void rfidListen_OutputChange(object sender, OutputChangeEventArgs e)
        {
            notify(new RFIDFrame(this, e.Index.ToString() + e.Value.ToString()));
        }

        public void testOutput()
        {
            if (rfidListen.Attached)
            {
                rfidListen.outputs[0] = !rfidListen.outputs[0];
                rfidListen.outputs[1] = !rfidListen.outputs[1];
            }
        }

        void rfidListen_Attach(object sender, AttachEventArgs e)
        {
            RFID rfid = (RFID)sender;
            RFIDFrame aFrame = new RFIDFrame(this);
            string outStr = TYPE_ATTACH + rfid.SerialNumber + "," + rfid.Type;
            aFrame.Data = outStr;
            notify(aFrame);         
            //Enable RFID Antenna
            rfid.Antenna = true;
        }

        void rfidListen_Detach(object sender, DetachEventArgs e)
        {
            notify(new RFIDFrame(this, TYPE_DETACH));
        }

        void rfidListen_Error(object sender, ErrorEventArgs e)
        {
            RFIDFrame aFrame = new RFIDFrame(this);
            aFrame.Data = TYPE_ERROR + e.Description;
            notify(aFrame);
        }

        void rfidListen_TagLost(object sender, TagEventArgs e)
        {
            /*
            notify(new RFIDFrame(this, string.Empty));

            RFID rfid = (RFID)sender;       
            rfid.LED = false;
            */
        }

        void rfidListen_Tag(object sender, TagEventArgs e)
        {
            notify(new RFIDFrame(this, e.Tag));

            RFID rfid = (RFID)sender;
            rfid.LED = true;
        }

        //Define the frame class for this library
        public class RFIDFrame : SensorFrame
        {
            public RFIDFrame(PhidgetUSBRFID_Lib parent) : base(parent)
            {
            }
            public RFIDFrame(PhidgetUSBRFID_Lib parent, string val) : base(parent)
            {
                Data = val;
            }
        }

        public override string ToString()
        {
            return GetType().Name;
        }
    }
}
